
package com.insigma.business.qrcode.soldier._provider.impl;



import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.*;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.soldier.entity.PlacementPbNewsDO;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier._provider.PlacementPbNewsProvider;
import com.insigma.business.qrcode.soldier.service.PlacementPbNewsService;
import com.insigma.business.qrcode.common.converter.PlacementPbNewsProviderConverter;
import com.insigma.business.qrcode.soldier.service.impl.PlacementPbNewsSearchRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@Component("placementPbNewsProvider")
public class PlacementPbNewsProviderImpl extends BaseProviderImpl implements PlacementPbNewsProvider {

    @Autowired
    private PlacementPbNewsService placementPbNewsService;


    @Override
    public BasePagingResponse<List<PlacementPbNewsDTO>> searchPlacementPbNews(final PlacementPbNewsSearchRequest searchRequest) {

        final BasePagingResponse<List<PlacementPbNewsDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页PlacementPbNews的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final PlacementPbNewsBSearcher placementPbNewsBSearcher = PlacementPbNewsProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<PlacementPbNewsDO> placementPbNewsDOList = placementPbNewsService.search(placementPbNewsBSearcher);
                final List<PlacementPbNewsDTO> placementPbNewsDTOList = PlacementPbNewsProviderConverter.convertResponseDOList2DTOList(placementPbNewsDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, placementPbNewsDTOList, searchRequest,placementPbNewsBSearcher.getTotalCount());
             }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_PB_NEWS_SEARCH;
            }
        });

        return basePagingResponse;
    }



    @Override
    public BaseResponse<PlacementPbNewsDTO> getPlacementPbNews(final BaseGetRequest baseGetRequest){

        final BaseResponse<PlacementPbNewsDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条PlacementPbNews的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final PlacementPbNewsDO placementPbNewsDO = placementPbNewsService.get((String)baseGetRequest.getId());
                if (placementPbNewsDO != null) {
                    placementPbNewsService.addReadCount(placementPbNewsDO.getId());
                }
                final PlacementPbNewsDTO remoteObj = PlacementPbNewsProviderConverter.convertResponseDO2DTO(placementPbNewsDO);
                ResponseUtils.setupSuccessResponse(baseResponse,remoteObj);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_PB_NEWS_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertPlacementPbNews(final PlacementPbNewsInsertRequest insertRequest){

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增PlacementPbNews的请求参数不能为空");
            }
            @Override
            public void executeService() {
                final PlacementPbNewsDO placementPbNewsDO = PlacementPbNewsProviderConverter.convertRequestIO2DO(insertRequest);
                placementPbNewsService.insert(placementPbNewsDO);
                ResponseUtils.setupSuccessResponse(baseResponse, placementPbNewsDO.getId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_PB_NEWS_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updatePlacementPbNews(final PlacementPbNewsUpdateRequest updateRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改PlacementPbNews的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(),"操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(),"操作人类型不能为空");
            }
            @Override
            public void executeService() {
                PlacementPbNewsDO placementPbNewsDO = PlacementPbNewsProviderConverter.convertRequestUO2DO(updateRequest);
                Integer affectRowNum = placementPbNewsService.update(placementPbNewsDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_PB_NEWS_UPDATE;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> deletePlacementPbNews(final BaseDeleteRequest deleteRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除PlacementPbNews的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(),"操作人类型不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getModifierId(),"操作人不能为空");
            }
            @Override
            public void executeService() {
                final Integer affectRowNum = placementPbNewsService.delete((String)deleteRequest.getId(),deleteRequest.getModifierType(),(String)deleteRequest.getModifierId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_PB_NEWS_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> likeNews(SimpleApiRequest request) {

        BizCheckUtil.checkNotNull(request, "请求不能为空");
        BizCheckUtil.checkNotNull(request.getId(), "新闻id不能为空");
        BizCheckUtil.checkNotNull(request.getLike(), "like不能为空");

        PlacementPbNewsDO placementPbNewsDO = placementPbNewsService.get(request.getId());
        BizCheckUtil.checkNotNull(placementPbNewsDO, "新闻不存在");
        Integer i = placementPbNewsService.likeOrNotNews(request.getId(), request.getLike());
        return ResponseUtils.buildSuccessResponse(i);
    }

    @Override
    public BaseResponse<Integer> publishNews(PlacementPbNewsUpdateRequest request) {

        BizCheckUtil.checkNotNull(request, "请求不能为空");
        BizCheckUtil.checkNotNull(request.getId(), "新闻id不能为空");
        BizCheckUtil.checkNotNull(request.getPublished(), "状态不能为空");

        PlacementPbNewsDO placementPbNewsDO = placementPbNewsService.get(request.getId());
        BizCheckUtil.checkNotNull(placementPbNewsDO, "新闻不存在");
        placementPbNewsDO.setPublished(request.getPublished());
        Integer update = placementPbNewsService.update(placementPbNewsDO);
        return ResponseUtils.buildSuccessResponse(update);
    }


}
